package com.v1.video.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.iss.view.pulltorefresh.PullToRefreshBase;
import com.iss.view.pulltorefresh.PullToRefreshBase.Mode;
import com.iss.view.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import com.iss.view.pulltorefresh.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.adapter.LiveListAdapter;
import com.v1.video.domain.VRLiveEntity;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;

/**
 * 直播列表
 * 
 * @author wangfang
 * 
 */
public class LiveListActivity extends BaseActivity implements OnClickListener {

	private static final String TAG = "LiveListActivity";
	private View mEmptyView;
	private TextView mTvTitle;
	private PullToRefreshListView mRefreshListView;
	private ListView mListView;
	private LiveListAdapter mAdapter;
	private int mCurIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_livelist);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
		MobclickAgent.onPageEnd("直播");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
		MobclickAgent.onPageStart("直播");
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mEmptyView = getLayoutInflater().inflate(R.layout.fragment_no_content,
				null);
		mTvTitle = (TextView) this.findViewById(R.id.tv_title);
		mRefreshListView = (PullToRefreshListView) findViewById(R.id.lv_listView);
		mRefreshListView.setScrollingWhileRefreshingEnabled(false);
		mRefreshListView.setMode(Mode.PULL_FROM_START);
		mListView = mRefreshListView.getRefreshableView();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.lay_result).setOnClickListener(this);
		mEmptyView.setOnClickListener(this);
		mRefreshListView.setOnRefreshListener(new OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				mCurIndex = 0;
				if (Helper.checkConnection(LiveListActivity.this)) {
					getServerData();
				} else {
					if (mRefreshListView != null) {
						mRefreshListView.onRefreshComplete();
					}
					showToast(LiveListActivity.this.getResources()
							.getString(R.string.net_no_notclick));
				}
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(LiveListActivity.this)) {
					getServerData();
				} else {
					if (mRefreshListView != null) {
						mRefreshListView.onRefreshComplete();
					}
					showToast(LiveListActivity.this.getResources()
							.getString(R.string.net_no_notclick));
				}
			}

		});
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		mTvTitle.setText(R.string.livelist_title_text);
		mAdapter = new LiveListAdapter(this);
		mListView.setAdapter(mAdapter);
		if (mRefreshListView != null) {
			mRefreshListView.doPullRefreshing(100);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lay_result:
			this.finish();
			break;
		case R.layout.fragment_no_content:
			if (mRefreshListView != null) {
				mRefreshListView.doPullRefreshing(100);
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 设置空的界面显示
	 */
	private void setEmptyView() {
		if (mCurIndex == 0) {
			View view = mListView.getEmptyView();
			if (view == null) {
				mListView.setEmptyView(mEmptyView);
			}
			if (mRefreshListView != null) {
				mRefreshListView.setMode(Mode.PULL_FROM_START);
			}
		}
	}

	private void getServerData() {
		String url = String.format(Constant.VR_LIVE_LIST, mCurIndex,
				Constant.HOME_WATERFULL_PAGE_SIZE, Constant.PRODUCT_CODE,
				Constant.PRODUCT_VERSION, Constant.DEVICEID);
		Logger.i(TAG, "VR直播url==" + url);
		RequestManager.getInstance().getRequest(this, url, VRLiveEntity.class,
				new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						// TODO Auto-generated method stub
						if (mRefreshListView != null) {
							mRefreshListView.onRefreshComplete();
						}
						if (object != null) {
							VRLiveEntity entity = (VRLiveEntity) object;
							if (entity != null && entity.getBody() != null
									&& entity.getBody().getData() != null) {
								if (mAdapter != null) {
									if (mCurIndex == 0) {
										mAdapter.setLstData(entity.getBody()
												.getData());
									} else {
										mAdapter.setAddLstData(entity.getBody()
												.getData());
									}
									if ((mCurIndex + 1) < entity.getBody()
											.getPage()) {
										mCurIndex++;
										if (mRefreshListView != null) {
											mRefreshListView.setMode(Mode.BOTH);
										}
									} else {
										if (mRefreshListView != null) {
											mRefreshListView
													.setMode(Mode.PULL_FROM_START);
										}
									}
								}
							} else {
								setEmptyView();
							}
						} else {
							setEmptyView();
						}
					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						if (mRefreshListView != null) {
							mRefreshListView.onRefreshComplete();
						}
						setEmptyView();
						Logger.i(TAG, message);
					}

				});
	}

}
